/*
 * Copyright 2017 JessYan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package me.jessyan.lifecyclemodel;

/**
 * {@link LifecycleModelProviders} 配合 {@link LifecycleModel} 的实现类可以帮助 {@link ohos.aafwk.ability.Ability} 和 {@link ohos.aafwk.ability.fraction.Fraction}
 * 储存存和管理一些与 UI 相关以及他们必需的数据, 并且这些数据在屏幕旋转或配置更改引起的 {@link ohos.aafwk.ability.Ability} 重建的情况下也会被保留, 直到最后被 finish
 * 但是 {@link LifecycleModel} 的实现类切勿直接引用 {@link ohos.aafwk.ability.Ability} 和 {@link ohos.aafwk.ability.fraction.Fraction} 以及他们里面 UI 元素
 * <p>
 * 他还可以让开发者能够在绑定在同一个 {@link ohos.aafwk.ability.Ability} 之下的多个不同的 {@link ohos.aafwk.ability.fraction.Fraction} 之间通讯以及共享数据 (Ability 与 Fraction 之间的通讯以及共享数据也可以)
 * 使用这种方式进行通讯, {@link ohos.aafwk.ability.fraction.Fraction} 之间不存在任何耦合关系
 * <p>
 * {@link LifecycleModel} 可以用来存储以及管理数据, 也可以作为 MVP 模式中的 Presenter (只要实现 {@link LifecycleModel} 即可)
 * 或者 MVVM 模式中的 ViewModel (只要实现 {@link LifecycleModel} 即可) 用来管理业务逻辑, 更多使用方式等着你去发现
 * <pre>
 * public class UserLifecycleModel implements LifecycleModel {
 *     private int id;
 *
 *     public UserLifecycleModel(int id) {
 *          this.id = id;
 *     }
 *
 *     void doAction() {
 *
 *     }
 * }
 *
 * public class MainAbilitySecondSlice extends AbilitySlice  {
 *
 *      public void onStart(Intent intent) {
 *         LifecycleModelStore store = LifecycleModelProviders.of((FractionAbility) getAbility(), ResourceTable.Id_stack_layout);
 *         store.put(UserLifecycleModel.class.getName(), new UserLifecycleModel());
 *     }
 *         public void onActive() {
 *         FractionAbility fractionAbility= (FractionAbility) getAbility();
 *         AFraction.createAAbilitySlice(fractionAbility.getFractionManager(),ResourceTable.Id_depen);
 *         bFraction= BFraction.createBAbilitySlice(fractionAbility.getFractionManager(),ResourceTable.Id_depen);
 *     }
 * }
 * </pre>
 * <p>
 * 只要 AFraction 和 BFraction 绑定在同一个 Ability 下, 并使用同一个 key, 那获取到的 UserLifecycleModel 就是同一个对象
 * 这时就可以使用这个 UserLifecycleModel 进行通讯 (Fraction 之间如何通讯? 比如说接口回调? 观察者模式?) 和共享数据
 * 这时 Fraction 之间并不知道彼此, 也不互相持有, 所以也不存在耦合关系
 * <p>
 * <pre>
 *
 *  public class AFraction extends Fraction {
 *     public void onStart(Intent intent) {
 *         UserLifecycleModel lifecycleModel = LifecycleModelProviders.of(getFractionAbility(),id).get(UserLifecycleModel.class.getName());
 *     }
 *  }
 *
 * public class BFraction extends Fraction {
 * public void sendFraction() {
 *         UserLifecycleModel lifecycleModel = LifecycleModelProviders.of(getFractionAbility(),id).get(UserLifecycleModel.class.getName());
 *     }
 * }
 *
 * </pre>
 */

public interface LifecycleModel {
    /**
     * 这个方法会在宿主 {@link ohos.aafwk.ability.Ability} 或 {@link ohos.aafwk.ability.fraction.Fraction} 被彻底销毁时被调用, 在这个方法中释放一些资源可以避免内存泄漏
     */
    @SuppressWarnings("WeakerAccess")
    void onCleared();
}



